<?php
namespace Company\Controller;

use Think\Controller;
/**
 * Created by PhpStorm.
 * User: 王越兴
 * Date: 2016/8/22 0022
 * Time: 上午 10:13
 */
class NewsController extends Controller
{
    
    public function getNews()
    {
        //  获取数据库新闻信息
        $news=D('news');
        $list=$news->select();
        $this->assign("list",$list);
        $this->display();

    }

    public function getNewsAddForm(){
        $this->display();
    }
    public function addNews(){

        //  添加新闻
        $title=$_POST["title"];
        $content=$_POST["content"];
        $news=D("news");
        if($title!=null&$content!=null){
            $data["title"]=$title;
            $data["content"]=$content;
            $re=$news->add($data);
            if(!empty($re)){
                $this->success('添加成功','getNews');
            }
            else{
                $this->error('添加失败','getNews');
            }
        }else{
            echo "内容不能为空！";
        }

    }

    public function upNews(){

        //  获取新闻列表id
        //  根据id获取数据库数据显示在表单
         $id=$_GET["id"];
         $d=D("news");
         $list=$d->where("id='$id'")->select();
         //var_dump($list);
         $this->assign("list",$list);
         $this->display();

    }
    //  跟新新闻数据库操作
    public function  edNews(){

        $id=$_GET["id"];
        $title=$_POST["title"];
        $content=$_POST["content"];
        $d=D("news");
        $data["title"]=$title;
        $data["content"]=$content;
        $re=$d->where("id='$id'")->save($data);
        if($re){
            $this->success('更改成功','getNews');
        }else{
            echo "更改失败";
        }

    }

    public function delete(){

        //  获取新闻列表id
        $id=$_GET["id"];
        //  根据id删除该行表格数据
        $delete=D("news");
        $de= $delete->execute("delete from ds_news where id='$id'");
        if(!empty($de)){
            $this->success('删除成功','/news/getNews');
        }
        else{
            $this->error('删除失败','/news/getNews');
        }

    }

}